<?php
namespace app\model;
use app\BaseModel;
use app\console\model\Admin;
use app\console\model\AdminRole;
use think\facade\Db;
use think\facade\Request;
use think\model\concern\SoftDelete;
use app\model\Member;
use app\model\ShopType;

class Shop extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort','status'];

    public function searchLngAttr($query, $value, $data)
    {

    }

    public function searchLatAttr($query, $value, $data)
    {

    }

    public function searchOrderAttr($query, $value, $data)
    {

    }

    public function searchMemberIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('member_id', '=', $value);
        }
    }

    public function searchTypeIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('type_id', '=', $value);
        }
    }

    public function searchIsHotAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('is_hot', '=', $value);
        }
    }

    public function searchAuditStatusAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('audit_status', '=', $value);
        }
    }


    protected function setMemberIdAttr($value, $data)
    {
        //当前字段的修改器
        $member = Member::where('id', $value)->find();
        $this->set('member_mobile', $member['mobile']);
        $this->set('member_name', $member['name']);
        return $value;
    }

    protected function setTypeIdAttr($value, $data)
    {
        //当前字段的修改器
        $this->set('type_title', ShopType::where('id', $value)->value('title'));
        return $value;
    }

    protected function setAuditStatusAttr($value, $data)
    {
        //当前字段的修改器
        $status = [1 => '待审核', 2 => '审核成功', 3 => '审核失败'];
        $this->set('audit_title', $status[$data['audit_status']]);
        return $value;
    }

    protected static function onAfterWrite($data)
    {
        $admin = Admin::where('admin_name', $data['member_mobile'])->find();
        if (empty($admin)) {
            $admin_id = Db::name('admin')->insertGetId(['admin_name' => $data['member_mobile'], 'admin_password' => 123456, 'data_id_str' => $data['id'], 'url' => 'console/shop/index']);
            Db::name('admin_role')->insertGetId(['admin_id' => $admin_id, 'role_id' => 5]);
        }

    }





}